package com.tebiecloud.order.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.order.client.model.entity.OrderStats2;
import com.tebiecloud.order.server.service.OrderStats2Service;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 门店营销额统计，维度：门店，年，月，日 前端控制器
 * @author tebie
 * @date 2019-10-07
 */
@RestController
@RequestMapping("/orderstats2")
public class OrderStats2Controller {

    @Autowired
    private OrderStats2Service orderStats2Service;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "list")
    public ResultBody<IPage<OrderStats2>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        OrderStats2 query = pageParams.mapToObject(OrderStats2.class);
        QueryWrapper<OrderStats2> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(orderStats2Service.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<OrderStats2> get(@RequestParam("id") Long id) {
        OrderStats2 entity = orderStats2Service.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param storeId 门店id,
     * @param year 年份,
     * @param month 月份,
     * @param day 日期,
     * @param amount 营业总额，单位分,
     * @param count 订单笔数,
     * @param writeoffAmount 核销总金额，单位分,
     * @param writeoffCount 核销笔数,
     * @param cashAmount 现金营业额，单位分,
     * @param cashCount 现金笔数,
     * @param swipeAmount 刷卡营业额，单位分,
     * @param swipeCount 刷卡笔数,
     * @param onlineAmount 线上营业额，单位分,
     * @param onlineCount 线上笔数
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "storeId", required = false) Long storeId,
        @RequestParam(value = "year", required = false) String year,
        @RequestParam(value = "month", required = false) String month,
        @RequestParam(value = "day", required = false) String day,
        @RequestParam(value = "amount", required = false) Integer amount,
        @RequestParam(value = "count", required = false) Integer count,
        @RequestParam(value = "writeoffAmount", required = false) Integer writeoffAmount,
        @RequestParam(value = "writeoffCount", required = false) Integer writeoffCount,
        @RequestParam(value = "cashAmount", required = false) Integer cashAmount,
        @RequestParam(value = "cashCount", required = false) Integer cashCount,
        @RequestParam(value = "swipeAmount", required = false) Integer swipeAmount,
        @RequestParam(value = "swipeCount", required = false) Integer swipeCount,
        @RequestParam(value = "onlineAmount", required = false) Integer onlineAmount,
        @RequestParam(value = "onlineCount", required = false) Integer onlineCount
    ) {
        OrderStats2 entity = new OrderStats2();
        entity.setStoreId(storeId);
        entity.setYear(year);
        entity.setMonth(month);
        entity.setDay(day);
        entity.setAmount(amount);
        entity.setCount(count);
        entity.setWriteoffAmount(writeoffAmount);
        entity.setWriteoffCount(writeoffCount);
        entity.setCashAmount(cashAmount);
        entity.setCashCount(cashCount);
        entity.setSwipeAmount(swipeAmount);
        entity.setSwipeCount(swipeCount);
        entity.setOnlineAmount(onlineAmount);
        entity.setOnlineCount(onlineCount);
        orderStats2Service.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param storeId 门店id
     * @param year 年份
     * @param month 月份
     * @param day 日期
     * @param amount 营业总额，单位分
     * @param count 订单笔数
     * @param writeoffAmount 核销总金额，单位分
     * @param writeoffCount 核销笔数
     * @param cashAmount 现金营业额，单位分
     * @param cashCount 现金笔数
     * @param swipeAmount 刷卡营业额，单位分
     * @param swipeCount 刷卡笔数
     * @param onlineAmount 线上营业额，单位分
     * @param onlineCount 线上笔数
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "storeId", required = false) Long storeId,
            @RequestParam(value = "year", required = false) String year,
            @RequestParam(value = "month", required = false) String month,
            @RequestParam(value = "day", required = false) String day,
            @RequestParam(value = "amount", required = false) Integer amount,
            @RequestParam(value = "count", required = false) Integer count,
            @RequestParam(value = "writeoffAmount", required = false) Integer writeoffAmount,
            @RequestParam(value = "writeoffCount", required = false) Integer writeoffCount,
            @RequestParam(value = "cashAmount", required = false) Integer cashAmount,
            @RequestParam(value = "cashCount", required = false) Integer cashCount,
            @RequestParam(value = "swipeAmount", required = false) Integer swipeAmount,
            @RequestParam(value = "swipeCount", required = false) Integer swipeCount,
            @RequestParam(value = "onlineAmount", required = false) Integer onlineAmount,
            @RequestParam(value = "onlineCount", required = false) Integer onlineCount
    ) {
        OrderStats2 entity = new OrderStats2();
        entity.setStoreId(storeId);
        entity.setYear(year);
        entity.setMonth(month);
        entity.setDay(day);
        entity.setAmount(amount);
        entity.setCount(count);
        entity.setWriteoffAmount(writeoffAmount);
        entity.setWriteoffCount(writeoffCount);
        entity.setCashAmount(cashAmount);
        entity.setCashCount(cashCount);
        entity.setSwipeAmount(swipeAmount);
        entity.setSwipeCount(swipeCount);
        entity.setOnlineAmount(onlineAmount);
        entity.setOnlineCount(onlineCount);
        orderStats2Service.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            orderStats2Service.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            orderStats2Service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
